package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.service.CheckGroupService;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);

        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage, pageSize);

        Page<CheckGroup> page = checkGroupDao.selectByCondition(queryString);

        long total = page.getTotal();
        List<CheckGroup> rows = page.getResult();

        return new PageResult(total, rows);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {

        Integer checkGroupId = checkGroup.getId();
        //根据检查组id删除中间表数据（清理原有关联关系）
        checkGroupDao.deleteAssociation(checkGroupId);

        //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
        this.setCheckGroupAndCheckItem(checkGroupId, checkitemIds);

        //更新检查组基本信息
        checkGroupDao.edit(checkGroup);
    }

    @Override
    public void deleteById(Integer id) {
        //先根据检查组id删除中间表数据（清理原有关联关系）
        checkGroupDao.deleteAssociation(id);

        //再删除t_checkgroup表的检查组数据
        checkGroupDao.deleteById(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {


            Map<String, Integer> map = null;

            for (Integer checkitemId : checkitemIds) {
                map = new HashMap<>();

                map.put("checkgroupId", checkGroupId);
                map.put("checkitemId", checkitemId);

                checkGroupDao.setCheckGroupAndCheckItem(map);
            }
        }
    }

}
